<?php


namespace app\admin\service;


use think\Model;

class User extends Model
{
    /**
     * data 用户登录后的返回的数据,删除时只需传入 [token => 11111]
     * data = [
     *      id = 1,
     *      nickname = '里里'，
     *      account  = ’xxx‘,
     *      token    = 'xxx',
     *      status   =  1
     *      menu     = []
     * ];
     * Desc: token在redis中的数据
     * User: 苏鹏
     * Date: 2023/9/21
     * Time: 17:48
     * @return void
     */

    public function tokenInfoInRedis(string $type = 'get', array $data = [])
    {

        switch ($type){
            case 'get' :
                return redis()->get('admin_token:'.$data['token']);
            case 'set' :
                if(empty($data))
                {
                    re_error_json(msg_code(18));
                }
                //将token解析信息放到redis,过期时间为token失效时间减去60秒
                $admin_token_config = get_config_cache('admin_token');
                $exp_time = $admin_token_config['exp'] - 60;
                redis()->set('admin_token:'.$data['token'],json_encode($data), $exp_time);
                break;
            case 'del' :
                redis()->del('admin_token:'.$data['token']);
                break;
        }
    }

    /**
     * @param string $type
     * @param int $user_id
     * @param string $token
     * Desc: token黑名单
     * User: 苏鹏
     * Date: 2023/9/21
     * Time: 21:29
     * @return false|mixed|object|\Redis|string|void
     * @throws \RedisException
     */
    public function userReTokenInRedis(string $type = 'get', int  $user_id = 0, string $token = '')
    {
        switch ($type){
            case 'get' :
                return redis()->get('admin_token:user_re_token_'.$user_id);
            case 'set' :
                $data[] = [
                    'token' => $token,
                    'create_time' => time()
                ];
                $result = redis()->get('admin_token:user_re_token_'.$user_id);
                $result = json_decode($result, true);
                if(!empty($result))
                {
                    $data = array_merge($data, $result);
                }

                redis()->set('admin_token:user_re_token_'.$user_id,json_encode($data));
                break;
            case 'del' :
                redis()->del('admin_token:user_re_token_'.$user_id);
                break;
            case 'get_all' :
                $result =  redis()->keys('admin_token:user_re_token_*');
                $data = [];
                foreach($result as $k=>$v)
                {
                    $blacklist = redis()->get($v);
                    if(!empty($blacklist)){
                        $blacklist = json_decode($blacklist, true);
                        foreach($blacklist as $key=>$val)
                        {
                            if(!empty($val['token']))
                            {
                                $data[] = $val['token'];
                            }
                        }
                    }
                }
                return $data;
        }
    }

    /**
     * $tokens = [
     *      'xxxxxxxxxx',
     *      'xxxxxxxxxx'
     * ]
     * Desc: token加入黑名单
     * User: 苏鹏
     * Date: 2023/9/22
     * Time: 10:18
     * @return void
     */
    public function tokenInBlacklist(string $type = 'get', array $tokens = [])
    {

        switch ($type){
            case 'get' :
                return redis()->get('admin_token:blacklist');
            case 'set' :
                if(!empty($tokens))
                {
                    $result = redis()->get('admin_token:blacklist');
                    if(!empty($result)){
                        $result = json_decode($result, true);
                        $tokens = array_merge($result, $tokens);
                    }
                    redis()->set('admin_token:blacklist',json_encode($tokens));
                }
                break;
            case 'del' :
                 //todo
                break;
        }
    }

    /**
     * Desc:用户退出 重新登录 或后台对用户密码修改 禁用 删除
     * 将用户在有效期的token全部加入黑名单，并清空关联关系
     * User: 苏鹏
     * Date: 2023/9/25
     * Time: 9:56
     * @return void
     */
    public function  clearTokenAndJoinBlacklist(int $user_id)
    {
        //将用户在有效期的token全部加入黑名单，并清空关联关系
        $user_re_token = $this->userReTokenInRedis('get', $user_id);
        $user_re_token = json_decode($user_re_token, true);
        if(!empty($user_re_token))
        {
            $token_blacklist = [];
            foreach($user_re_token as $k=>$v)
            {
                $token_blacklist[] = $v['token'];
            }
            $this->tokenInBlacklist('set', $token_blacklist); //将用户所有token全部加入黑名单
            //删除token缓存列表中此用户所有token缓存
            foreach($user_re_token as $key=>$val)
            {
                $this->tokenInfoInRedis('del', ['token'=>$val['token']]);
            }
            $this->userReTokenInRedis('del', $user_id);  //删除用户与token所有关联关系释放内存
        }
    }


    /**
     * Desc: 获取用户信息
     * User: 苏鹏
     * Date: 2023/11/8
     * Time: 13:50
     * @return void
     */
    public function getUserInfo(int $id)
    {
        if(empty($id))
        {
            return [];
        }
        //todo 权限中心用户信息
        return [
            'name' => '测试用户'
        ];
    }

    public function getUserName(int $id)
    {
        $result = $this->getUserInfo($id);
        return $result['name'];
    }
}